package org.bluedream.core.module.controller.goods;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.comm.utils.RestResult;
import org.bluedream.comm.utils.excel.ExcelUtil;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.entity.goods.Goods;
import org.bluedream.core.module.entity.goods.Product;
import org.bluedream.core.module.service.goods.GoodsService;
import org.bluedream.core.module.service.goods.ProductService;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName depItemUserController
 * @Description TODO
 * @Author foxsand
 * @Data 2021-07-26 8:59
 * @Version
 */
@Slf4j
@Controller
@RequestMapping(value = "${adminPath}/goods")
public class GoodsController extends BaseController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private ProductService productService;

    @ModelAttribute
    public Goods get(String id){
        return goodsService.getById(id);
    }

    @RequiresPermissions("goods:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(Goods entity, Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("goods" , entity);
        return "module/goods/goodsList";
    }

    @RequiresPermissions("goods:view")
    @RequestMapping(value = {"/clsGoodsList"})
    public String clsGoodsList(Goods entity, Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("goods" , entity);
        return "module/goods/clsGoodsList";
    }

    @RequiresPermissions("goods:view")
    @RequestMapping("/form")
    public String form(Goods entity, Model model){
        model.addAttribute("goods" , entity);
        return "module/goods/goodsForm";
    }

    @RequiresPermissions("goods:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<Goods> findPage(@RsaSecurityParameter Goods entity){
        return goodsService.findPage(entity);
    }

    @RequiresPermissions("goods:view")
    @RequestMapping("/findPage4GdQpc")
    @ResponseBody
    public IPage<Product> findPage4GdQpc(@RsaSecurityParameter Goods entity){
        return productService.findPage(entity);
    }

    @RequiresPermissions("goods:view")
    @RequestMapping("/getGoods")
    @ResponseBody
    public RestResult getGoods(Goods entity){
        return goodsService.getOne(entity);
    }

    @RequiresPermissions("goods:edit")
    @RequestMapping("/save")
    @ResponseBody
    public RestResult save(Goods entity){
       return goodsService.saveEntity(entity);
    }

    /**
     * 导出excel演示
     * @param request
     * @param resp
     */
    @RequiresPermissions("goods:view")
    @RequestMapping(value = "/fileDownload" , method = RequestMethod.GET)
    public void fileDownload(HttpServletRequest request, HttpServletResponse resp){
        try {
            List<Goods> goodsList = goodsService.list();
            ExcelUtil.export4List(resp , goodsList , Goods.class , "exportFileName" , "exportSheetName" , "测试Excel文件导出");
        }catch (Exception e){
            e.getMessage();
        }
    }

    /**
     * 导入excel演示
     * @param file
     */
    @RequiresPermissions("goods:view")
    @RequestMapping(value = "/excelImport")
    @ResponseBody
    public RestResult excelImport(@RequestParam("file") MultipartFile file){
        return goodsService.excelImport(file);
    }

    @RequiresPermissions("goods:view")
    @RequestMapping(value = "/createBarcode")
    @ResponseBody
    public RestResult createBarcode(){
        return RestResult.ok(goodsService.barcode("001004001"));
    }
}
